/**
 * Rubik is a for-testing-purposes simple CMS.
 * Copyright (C) 2010 Antonio Hernandez Diaz <ahdiaz@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * $Id: Test_Articles.java 8 2010-04-13 19:10:09Z ahdiaz $
 */

package net.ahdiaz.rubik.tests;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBException;

import net.ahdiaz.rubik.model.ArticlesCollection;
import net.ahdiaz.rubik.model.PagesCollection;
import net.ahdiaz.rubik.model.generated.Article;
import net.ahdiaz.rubik.model.generated.ArticleData;
import net.ahdiaz.rubik.model.generated.ArticleRef;
import net.ahdiaz.rubik.model.generated.Author;
import net.ahdiaz.rubik.model.generated.Authors;
import net.ahdiaz.rubik.model.generated.Creator;
import net.ahdiaz.rubik.model.generated.Editor;
import net.ahdiaz.rubik.model.generated.Metadata;
import net.ahdiaz.rubik.model.generated.Page;
import net.ahdiaz.rubik.model.generated.PageData;
import net.ahdiaz.rubik.model.generated.RelatedArticles;
import net.ahdiaz.rubik.model.generated.Tag;
import net.ahdiaz.rubik.model.generated.Tags;
import ru.ispras.sedna.driver.DriverException;

/**
 * Clean up the database and create default content
 * using the JAXB generated classes.
 */
public class Test_Articles {

	public static void main(String[] args) {
		new Test_Articles();
	}
	
	public Test_Articles() {
		try {
			
			this.createArticles();
			this.createPages();
			
		} catch (DriverException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String getTimestamp() {
		Date date = new Date();
		return ((Long)date.getTime()).toString();
	}
	
	private void createArticles() throws DriverException, JAXBException {
		
		ArticlesCollection articles = new ArticlesCollection();
		Metadata metadata = new Metadata();
		ArticleData data = new ArticleData();
		Article article = new Article();
		Author author = null;
		Editor editor = new Editor();
		Tag tag = null;
		
		Collection<Article> c = articles.readAll();
		Iterator<Article> it = c.iterator();
		while (it.hasNext()) {
			articles.remove(it.next());
		}
		
		metadata.setCreator(new Creator());
		metadata.getCreator().setUid("dcela");
		metadata.getCreator().setValue("D. Cela");
		metadata.setCreationDate(this.getTimestamp());
		metadata.setLastUpdated(this.getTimestamp());
		metadata.setTags(new Tags());
		tag = new Tag();
		tag.setName("economy");
		metadata.getTags().getTag().add(tag);
		tag = new Tag();
		tag.setName("politics");
		metadata.getTags().getTag().add(tag);
		
		data.setAuthors(new Authors());
		author = new Author();
		author.setUid("dcela");
		author.setValue("D. Cela");
		data.getAuthors().getAuthor().add(author);
		author = new Author();
		author.setUid("mgarcia");
		author.setValue("M. Garcia");
		data.getAuthors().getAuthor().add(author);
		editor.setUid("mlopez");
		editor.setValue("M. Lopez");
		data.setEditor(editor);
		data.setPublicationDate(this.getTimestamp());
		data.setRelatedArticles(new RelatedArticles());
		
		data.setTitle("Some title for the article");
		data.setAbstract("Some abstract for article");
		data.setBody("This is the body");
		
		article.setVersion("1.0");
		article.setUid("A"+this.getTimestamp()+"-1");
		article.setMetadata(metadata);
		article.setArticleData(data);
		
		articles.append(article);
		
		metadata = new Metadata();
		data = new ArticleData();
		article = new Article();
		author = null;
		editor = new Editor();
		tag = null;
		
		metadata.setCreator(new Creator());
		metadata.getCreator().setUid("dcela");
		metadata.getCreator().setValue("D. Cela");
		metadata.setCreationDate(this.getTimestamp());
		metadata.setLastUpdated(this.getTimestamp());
		metadata.setTags(new Tags());
		tag = new Tag();
		tag.setName("economy");
		metadata.getTags().getTag().add(tag);
		tag = new Tag();
		tag.setName("politics");
		metadata.getTags().getTag().add(tag);
		
		data.setAuthors(new Authors());
		author = new Author();
		author.setUid("dcela");
		author.setValue("D. Cela");
		data.getAuthors().getAuthor().add(author);
		editor.setUid("mlopez");
		editor.setValue("M. Lopez");
		data.setEditor(editor);
		data.setPublicationDate(this.getTimestamp());
		data.setRelatedArticles(new RelatedArticles());
		
		data.setTitle("Some title for the article");
		data.setAbstract("Some abstract for article");
		data.setBody("This is the body");
		
		article.setVersion("1.0");
		article.setUid("A"+this.getTimestamp()+"-2");
		article.setMetadata(metadata);
		article.setArticleData(data);
		
		articles.append(article);
	}
	
	private void createPages() throws DriverException, JAXBException {
	
		PagesCollection pages = new PagesCollection();
		
		Collection<Page> c = pages.readAll();
		Iterator<Page> it = c.iterator();
		while (it.hasNext()) {
			pages.remove(it.next());
		}
		
		Page p = new Page();
		Metadata m = new Metadata();
		PageData d = new PageData();
		
		Creator creator = new Creator();
		creator.setUid("mlopez");
		creator.setValue("M. Lopez");
		m.setCreator(creator);
		m.setCreationDate(this.getTimestamp());
		m.setLastUpdated(this.getTimestamp());
		
		List<ArticleRef> refs = d.getArticleRef();
		
		ArticlesCollection articles = new ArticlesCollection();
		Collection<Article> ca = articles.readAll();
		Iterator<Article> ita = ca.iterator();
		while (ita.hasNext()) {
			Article a = ita.next();
			ArticleRef ref = new ArticleRef();
			ref.setUid(a.getUid());
			refs.add(ref);
		}
		
		p.setUid("P"+this.getTimestamp());
		p.setMetadata(m);
		p.setPageData(d);
		
		pages.append(p);
	}

}
